package tankproject;

import java.awt.*;
import java.util.*;
import java.awt.geom.Point2D;

public class Tank {

    private Point position = new Point(300, 250);
    private double speed = 0;
    private int ammo = 50;
    private double fuel = 500;
    private int degRotation = 0;
    private Game game;

    private ArrayList bullets = new ArrayList();

    public Tank(Game game) {
        this.game = game;
    }

    public void fire() throws OutOfAmmoException {
        if (ammo == 0)
            throw new OutOfAmmoException();
        ammo--;
        bullets.add(new Bullet(game, position.x + 31, position.y + 37));
    }

    public void speedUp() {
        if (speed >= 2)
            speed = 2;
        else
            speed += 0.5;
    }

    public void slowDown() {
        if (speed <= 0)
            speed = 0;
        else
            speed -= 0.5;
    }

    public void turnLeft() {
        degRotation -= 90;
    }

    public void turnRight() {
        degRotation += 90;
    }

    public void update() throws OutOfFuelException {
        move();
    }

    private void move() throws OutOfFuelException {
        if (fuel <= 0)
            throw new OutOfFuelException();
        fuel -= 0.1 * speed;

        float distancex = (float) Math.cos(Math.toRadians(degRotation));
        float distancey = (float) Math.sin(Math.toRadians(degRotation));
        position.x += distancex * speed * 2;
        position.y += distancey * speed * 2;
    }

    public Point getPosition() {
        collisionDetection();
        return position;
    }
    public void collisionDetection(){

        if (position.x<9)
            position.x=9;
        else if (position.x>710)
            position.x=710;
        else if (position.y<5)
            position.y=5;
        else if (position.y>482)
            position.y=482;
    }

    /**
     * The tank's rotation in radians.
     */
    public double getRotation() {
        return Math.toRadians(degRotation);
    }

    public double percentFuelRemaining() {
        return fuel / 500;
    }

    public ArrayList getBullets() {
        return bullets;
    }

}
